/*
 * Created: Author: Aléx Carvalho / Date: Mar 14, 2015
 * 
 * Changed: $Author: ac280593@gmail.com $ / $Date: 2015-06-11 04:31:49 +0000 (Thu, 11 Jun 2015) $
 */
package com.cpm.view.zk.applications;

import com.cpm.view.Element;
import com.cpm.view.zk.MenuAction;
import com.cpm.view.zk.parts.MenuBar;
import com.cpm.view.Icon;
import java.util.LinkedHashSet;
import java.util.Optional;
import org.zkoss.zk.ui.HtmlBasedComponent;
import org.zkoss.zul.Borderlayout;
import org.zkoss.zul.Center;
import org.zkoss.zul.West;
import com.cpm.view.zk.editors.AbstractEditor;
import org.zkoss.zul.North;

public abstract class AbstractApplication implements Element<HtmlBasedComponent> {

    private final LinkedHashSet<MenuAction> menuActions = new LinkedHashSet<>();

    private final MenuBar menuBar = new MenuBar();
    private final Borderlayout borderLayout = new Borderlayout();
    private final Center innerCenter = new Center();
    
    private boolean initializedComponents = false;
    private boolean initializedData = false;

    protected abstract void getMenuActions(LinkedHashSet<MenuAction> menuActions);

    protected abstract HtmlBasedComponent getComponentCenter();

    public abstract String getLabel();

    public abstract Icon getIcon();

    public abstract Double getPosition();

    protected void initComponents(Borderlayout borderlayout) {
        if (!initializedComponents) {
            updateCenter();

            innerCenter.setBorder("none");
            
            Center center = new Center();
            center.setBorder("none");

            North innerNorth = new North();
            innerNorth.appendChild(menuBar.getElement());
            innerNorth.setBorder("none");
            
            Borderlayout centerBorderLayout = new Borderlayout();
            centerBorderLayout.setVflex("true");
            centerBorderLayout.setHflex("true");
            centerBorderLayout.appendChild(innerNorth);
            centerBorderLayout.appendChild(innerCenter);

            center.appendChild(centerBorderLayout);
            borderlayout.appendChild(center);

            getWest().ifPresent(west -> borderlayout.appendChild(west));

            initializedComponents = true;
        }
    }

    protected void updateCenter() {
        menuActions.clear();
        getMenuActions(menuActions);
        menuBar.setMenuItems(menuActions);
        menuBar.updateLayout();

        innerCenter.getChildren().clear();
        innerCenter.appendChild(getComponentCenter());
    }

    @Override
    public HtmlBasedComponent getElement() {
        initComponents(borderLayout);
        performInitializationData();
        return borderLayout;
    }

    public void refreshContent() {
    }

    public Optional<West> getWest() {
        return Optional.empty();
    }

    protected void initializeData() {
    }

    private void performInitializationData() {
        if (!initializedData) {
            initializeData();
            initializedData = true;
        }
    }

    protected void showEditor(AbstractEditor editor) {
        menuActions.clear();
        editor.getMenuActions(menuActions);
        menuBar.setMenuItems(menuActions);
        menuBar.updateLayout();

        innerCenter.getChildren().clear();
        HtmlBasedComponent c = editor.getElement();
        c.setVflex("true");
        innerCenter.appendChild(c);
    }
}
